import { sidebarRoutes, errorRoutes, fixedRoutes } from "../../utils/checkRoute.js"
import { routes, userRoutes, adminRoutes } from '../../router/index.js'
const state = {
	sidebar: [],
	fixedRoutes: [],
}

const mutations = {
	SET_SIDEBAR: (state, sidebar) => {
		state.sidebar = sidebar
	},
	SET_FIXED: (state, fixed) => {
		state.fixed = fixed
	}
}

const actions = {
	getRoutes({ commit, dispatch }, role) {
		let newRoutes = []
		let otherRoutes = []
		if (role.includes('admin')) {
			newRoutes = [...routes, ...adminRoutes]
			otherRoutes = adminRoutes
		} else {
			newRoutes = [...routes, ...userRoutes]
			otherRoutes = userRoutes
		}
		errorRoutes(newRoutes)
		dispatch('getSidebar', sidebarRoutes(newRoutes))
		dispatch('getFixed', fixedRoutes(newRoutes))
		return new Promise((resolve, reject) => {
			resolve(otherRoutes)
		})
	},
	getSidebar({ commit }, routes) {
		commit('SET_SIDEBAR', routes)
	},
	getFixed({ commit }, routes) {
		commit('SET_FIXED', routes)
	}

}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
